package com.wocao.utils;

import com.wocao.entity.SysArea;
import com.wocao.entity.SysDept;
import com.wocao.entity.SysDept;
import com.wocao.entity.SysMenu;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Tree {

    //封装菜单树
    public static List<SysMenu> menusToTree(List<SysMenu> treeList) {
        //操作所有组织机构数据
        Map<Long, List<SysMenu>> groupMap = treeList.stream().collect(Collectors.groupingBy(SysMenu::getParentId));
        treeList.forEach(sysMenu-> {
            sysMenu.setChildMenu(groupMap.get(sysMenu.getMenuId()));
//            sysMenu.setChildSysMenu(groupMap.get(sysMenu.getMenuId()));
        });
        List<SysMenu> collect = treeList.stream().filter(sysMenu-> sysMenu.getParentId().equals(0L)).collect(Collectors.toList());
        return collect;
    }

    //封装部门树
    public static List<SysDept> deptTree(List<SysDept> treeList) {
        //操作所有组织机构数据
        Map<Long, List<SysDept>> groupMap = treeList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
        treeList.forEach(sysDept-> {
            sysDept.setChildDept(groupMap.get(sysDept.getDeptId()));
        });
        List<SysDept> collect = treeList.stream().filter(sysDept-> sysDept.getParentId().equals(0L)).collect(Collectors.toList());
        return collect;
    }

    //封装部门树进阶版
    public static List<SysDept> deptTree(List<SysDept> treeList, Long start) {
        //操作所有组织机构数据
        Map<Long, List<SysDept>> groupMap = treeList.stream().collect(Collectors.groupingBy(SysDept::getParentId));
        treeList.forEach(sysDept-> {
            sysDept.setChildDept(groupMap.get(sysDept.getDeptId()));
        });
        List<SysDept> collect = treeList.stream().filter(sysDept-> sysDept.getParentId().equals(start)).collect(Collectors.toList());
        return collect;
    }

    //封装地址树
    public static List<SysArea> areaTree(List<SysArea> treeList) {
        //操作所有组织机构数据
        Map<Long, List<SysArea>> groupMap = treeList.stream().collect(Collectors.groupingBy(SysArea::getParentId));
        treeList.forEach(SysArea-> {
            SysArea.setChildArea(groupMap.get(SysArea.getCode()));
        });
        List<SysArea> collect = treeList.stream().filter(SysArea-> SysArea.getParentId().equals(0L)).collect(Collectors.toList());
        return collect;
    }
}
